import aes from 'crypto-js/aes';
import CryptoJS from 'crypto-js';
import secret from '../../../../../utils/secret';

const getDecrypt = (params, secrets) =>
    JSON.parse(
        aes
            .decrypt(params, CryptoJS.enc.Utf8.parse(secrets), {
                iv: CryptoJS.enc.Utf8.parse(secrets),
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            })
            .toString(CryptoJS.enc.Utf8)
    );

const allRows = [
    {
        id: '11',
        taxLevel: '1',
        taxStartAmt: null,
        taxEndAmt: 36000,
        taxRate: 3,
        quickDeductAmt: 0
    },
    {
        id: '12',
        taxLevel: '2',
        taxStartAmt: 36000,
        taxEndAmt: 144000,
        taxRate: 1,
        quickDeductAmt: 2520
    },
    {
        id: '13',
        taxLevel: '3',
        taxStartAmt: 144000,
        taxEndAmt: 30000,
        taxRate: 2,
        quickDeductAmt: 16920
    },
    {
        id: '14',
        taxLevel: '4',
        taxStartAmt: 144000,
        taxEndAmt: null,
        taxRate: 2,
        quickDeductAmt: 16920
    }
];

const search = (/* request */) => ({
    code: 10000,
    data: {
        id: '01',
        name: '个人所得税预扣率表一',
        processStatus: 1,
        tableData: allRows
    }
});

const saveEdit = request => {
    const params = getDecrypt(request.body.params, secret);
    return {
        code: 10000,
        data: { params }
    };
};

export default {
    'GET x/api/finance/salary/v1/setting/tax/list': (req, res) => {
        res.json(search(req));
    },

    'POST x/api/finance/salary/v1/setting/tax': (req, res) => {
        res.json(saveEdit(req));
    },

    'PUT x/api/finance/salary/v1/setting/tax': (req, res) => {
        res.json(saveEdit(req));
    },

    'POST x/api/finance/salaryCalculate/deleteLevel': (req, res) => {
        res.json(saveEdit(req));
    },

    'DELETE x/api/finance/salary/v1/setting/tax/*/*': (req, res) => {
        res.json(saveEdit(req));
    }
};
